package com.sosee.cgfw.friends;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sosee.cgfw.R;
import com.sosee.cgfw.utils.Constans;
import com.sosee.cgfw.utils.Utils;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import io.rong.imkit.RongIM;

/**
 * Created by Thinkpad on 2016/3/6.
 */
public class FriendsSearch extends Activity implements View.OnClickListener {
    private  EditText friends_search_http;
    private TextView friends_search_ok;
    ImageButton friends_back8;
    private HttpUtils httpUtils;
    private List<NearData.Data> data;
    private LinearLayout teladdfails;
    private LinearLayout teladdfriends;

    private TextView my_names_search;
    private TextView my_nicknames_search;
    private TextView my_tels_search;
    private  TextView my_companys_search;
    private TextView my_dutys_search;
    private  Button my_messages_search;
    private String status;
    List<String> cityName = new ArrayList<String>();

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friends_searchhttp);
        httpUtils =new HttpUtils();
        initSearch();
    }

    private void initSearch() {
        friends_search_http = (EditText)findViewById(R.id.friends_search_http);
        friends_search_ok=(TextView)findViewById(R.id.friends_search_ok);
        friends_back8= (ImageButton)findViewById(R.id.friends_back8);
        teladdfails=(LinearLayout) findViewById(R.id.teladdfails);
        teladdfriends=(LinearLayout) findViewById(R.id.teladdfriends);

        my_names_search= (TextView)findViewById(R.id.my_names_search);
        my_nicknames_search= (TextView)findViewById(R.id.my_nicknames_search);
        my_tels_search= (TextView)findViewById(R.id.my_tels_search);
         my_companys_search= (TextView)findViewById(R.id.my_companys_search);
        my_dutys_search= (TextView)findViewById(R.id.my_dutys_search);
         my_messages_search= (Button)findViewById(R.id.my_messages_search);




        my_messages_search.setOnClickListener(this);
        friends_back8.setOnClickListener(this);
        friends_search_ok.setOnClickListener(this);


        initView();
    }

    private void initView() {

        httpUtils.send(HttpRequest.HttpMethod.GET, Constans.GETFRIENDSLIST_URL+Constans.userID, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                System.out.println("我是好友列表返回的数据：" + result);


                Gson gson = new Gson();
                FriendsListData friendsListData = gson.fromJson(result, FriendsListData.class);
                if("success".equals(friendsListData.getStatus())){

                    List<FriendsListData.Data> datas = friendsListData.getData();

                    for (int i = 0; i < datas.size(); i++) {
                        String ID = datas.get(i).getFriends().getID();
                        //吧名字赋值给name
                        cityName.add(ID);
                    }

                }else{

                }

            }

            @Override
            public void onFailure(HttpException e, String s) {
                Utils.showToast("网络加载异常");
            }
        });

    }

    public boolean flag= false;
    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.friends_search_ok:
                String trim = friends_search_http.getText().toString().trim();
                if(trim==null||trim.length()<11){
                    Utils.showToast("电话号码不正确");
                    return;
                }
                teladdfriends.setVisibility(View.GONE);
                teladdfails.setVisibility(View.GONE);
//                httpUtils.configCurrentHttpCacheExpiry(100);

                httpUtils.send(HttpRequest.HttpMethod.GET, Constans.GETUSERPHONE_URL+trim, new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String s = responseInfo.result;
                        System.out.println("这是获取电话后的信息" + s);
                        Gson gson = new Gson();
                        NearData nearData = gson.fromJson(s, NearData.class);
                        data = nearData.getData();

                        if("success".equals(nearData.getStatus())){
                                teladdfriends.setVisibility(View.VISIBLE);
                            //找到按钮设置值
                            my_names_search.setText(data.get(0).getRealname());
                            my_nicknames_search.setText(data.get(0).getNickname());
                            my_tels_search.setText(data.get(0).getPhone());
                            my_companys_search.setText(data.get(0).getCompany());
                            my_dutys_search.setText(data.get(0).getDuty());

                            if(Constans.userID.equals(data.get(0).getID())){
                                my_messages_search.setVisibility(View.GONE);
                            }
                            for (int i=0;i<cityName.size();i++){

                                if(data.get(0).getID().equals(cityName.get(i))){
                                    my_messages_search.setText("发送消息");
                                }else{
                                    my_messages_search.setText("加为好友");
                                }
                            }

                        }else{
                            teladdfails.setVisibility(View.VISIBLE);
                        }


                    }

                    @Override
                    public void onFailure(HttpException e, String s) {

                        Utils.showToast("网络获取异常");
                    }
                });

                break;
            case R.id.friends_back8:
                finish();

                break;
            case R.id.my_messages_search:

                if("加为好友".equals(my_messages_search.getText().toString())){

                httpUtils.send(HttpRequest.HttpMethod.GET, Constans.ADDFRIENDS_URL + Constans.userID + "&friendsID=" + data.get(0).getID(), new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String s = responseInfo.result;
                        System.out.println("这是返回的值" + s);
                        //发送消息到主线程通知更改  加为好友为等待验证。
                        Utils.showToast("请求已经发送");
                        finish();

                    }

                    @Override
                    public void onFailure(HttpException e, String s) {

                        Utils.showToast("网络获取异常");
                    }
                });
                }else{
                    if (RongIM.getInstance() != null) {
                        RongIM.getInstance().startPrivateChat(FriendsSearch.this, data.get(0).getID(), "hello");
                }}

                break;
            default:

                break;
        }
    }
}
